/*
 *    Copyright (c) 2018-2025, orient All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: orient
 */

package cn.goswan.orient.guohur.controller;

import cn.goswan.orient.common.core.constant.SecurityConstants;
import cn.goswan.orient.guohur.api.feign.RemoteDictItemService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.goswan.orient.common.core.util.R;
import cn.goswan.orient.common.log.annotation.SysLog;
import cn.goswan.orient.guohur.entity.Goods;
import cn.goswan.orient.guohur.service.GoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 商品表
 *
 * @author wangchuang
 * @date 2020-05-30 08:47:39
 */
@RestController
@AllArgsConstructor
@RequestMapping("/goods" )
@Api(value = "goods", tags = "商品表管理")
public class GoodsController {

    private final  GoodsService goodsService;
    private final RemoteDictItemService RemoteDictItemService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param goods 商品表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getGoodsPage(Page page, Goods goods) {
        return R.ok(goodsService.page(page, Wrappers.query(goods)));
    }


    /**
     * 通过id查询商品表
     * @param goodsCode id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{goodsCode}" )
    public R getById(@PathVariable("goodsCode" ) String goodsCode) {
        return R.ok(goodsService.getById(goodsCode));
    }

    /**
     * 新增商品表
     * @param goods 商品表
     * @return R
     */
    @ApiOperation(value = "新增商品表", notes = "新增商品表")
    @SysLog("新增商品表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('guohur_goods_add')" )
    public R save(@RequestBody Goods goods) {
        return R.ok(goodsService.save(goods));
    }

    /**
     * 修改商品表
     * @param goods 商品表
     * @return R
     */
    @ApiOperation(value = "修改商品表", notes = "修改商品表")
    @SysLog("修改商品表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('guohur_goods_edit')" )
    public R updateById(@RequestBody Goods goods) {
        return R.ok(goodsService.updateById(goods));
    }

    /**
     * 通过id删除商品表
     * @param goodsCode id
     * @return R
     */
    @ApiOperation(value = "通过id删除商品表", notes = "通过id删除商品表")
    @SysLog("通过id删除商品表" )
    @DeleteMapping("/{goodsCode}" )
    @PreAuthorize("@pms.hasPermission('guohur_goods_del')" )
    public R removeById(@PathVariable String goodsCode) {
        return R.ok(goodsService.removeById(goodsCode));
    }

	/**
	 * 查询商品类别
	 * @return R
	 */
	@ApiOperation(value = "查询商品类别", notes = "查询商品类别")
	@GetMapping("/type" )
	public R getGoodsType(){
		return RemoteDictItemService.getDictItems("goods_type", SecurityConstants.FROM_IN);
	}
	/**
	 * 查询商品单位
	 * @return R
	 */
	@ApiOperation(value = "查询商品单位", notes = "查询商品单位")
	@GetMapping("/unit" )
	public R getUnit(){
		return RemoteDictItemService.getDictItems("weight_unit,quantity_unit", SecurityConstants.FROM_IN);
	}




}
